/*
 * @Date: 2023-03-23 18:19:50
 * @LastEditTime: 2023-03-27 16:31:57
 */
Page({
  // 页面数据
  data: {
    device: {},
    deviceId: '',
    services: [],
    characteristics: [],
    isConnected: false,
  },

  // 页面载入时
  onLoad(e) {
    this.getSystemInfo();
  },

  go() {
    wx.navigateTo({
      url: '/pages/blePeripheralServer/blePeripheralServer',
    });
  },

  getSystemInfo() {
    wx.getSystemInfo({
      success: (res) => {
        if (res.bluetoothEnabled && res.locationEnabled)
          this.openBluetoothAdapter();
      },
    });
  },

  openBluetoothAdapter() {
    wx.openBluetoothAdapter({
      success: (res) => {
        this.onBluetoothDeviceFound();
        this.startBluetoothDevicesDiscovery();
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },

  closeBluetoothAdapter() {
    wx.closeBluetoothAdapter({
      success: (res) => {
        console.log(res);
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },

  startBluetoothDevicesDiscovery() {
    wx.startBluetoothDevicesDiscovery({
      // 筛选设备，去除骚扰设备列表数据
      services: ['0000ff11-0000-1000-8000-00805f9b34fb'],
      success: (res) => {
        console.log(res);
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },

  stopBluetoothDevicesDiscovery() {
    wx.stopBluetoothDevicesDiscovery();
  },

  onBluetoothDeviceFound() {
    wx.onBluetoothDeviceFound((res) => {
      if (res.devices?.length) {
        const device = res.devices[0];
        const deviceId = device.deviceId;
        this.setData({ device, deviceId }, () => {
          this.createBLEConnection();
        });
      }
    });
  },

  offBluetoothDeviceFound() {
    wx.offBluetoothDeviceFound();
  },

  createBLEConnection() {
    this.offBluetoothDeviceFound();

    const { deviceId } = this.data;
    wx.createBLEConnection({
      deviceId,
      timeout: 60000,
      success: (res) => {
        console.log(res);
        this.setData({ isConnected: true });
        this.stopBluetoothDevicesDiscovery();
        this.offBluetoothDeviceFound();
        this.getBLEDeviceServices();
      },
      fail: (err) => {
        console.log(err);
        this.onBluetoothDeviceFound();
      },
    });
  },

  getBLEDeviceServices() {
    const { deviceId } = this.data;
    wx.getBLEDeviceServices({
      deviceId,
      success: (res) => {
        this.setData({ services: res.services }, () => {
          this.getBLEDeviceCharacteristics();
        });
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },

  getBLEDeviceCharacteristics() {
    const { deviceId, services } = this.data;
    const serviceId = services[0].uuid;

    wx.getBLEDeviceCharacteristics({
      deviceId,
      serviceId,
      success: (res) => {
        console.log(res);
        this.setData({ characteristics: res.characteristics });
      },
      fail: (err) => {
        console.log(err);
      },
    });
  },

  writeBLECharacteristicValue() {
    const { deviceId, services, characteristics } = this.data;
    const serviceId = services[0].uuid;
    const characteristicId = characteristics[0].uuid;

    // 向蓝牙设备发送一个0x00的16进制数据
    let buffer = new ArrayBuffer(1);
    let dataView = new DataView(buffer);
    dataView.setUint8(0, 0);

    wx.writeBLECharacteristicValue({
      deviceId,
      serviceId,
      characteristicId,
      value: buffer,
      success(res) {
        console.log(res);
        wx.showToast({ title: '写入成功！' });
      },
    });
  },

  onHide() {
    this.closeBluetoothAdapter();
  },
});
